package com.spzx.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SkuCacheKeyUtils;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.domain.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 会员Service业务层处理
 *
 * @author lee
 * @date 2024-07-23
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public Integer register(UserInfo userInfo) {

        Long count = baseMapper.selectCount(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, userInfo.getPhone()));
        if (count > 0) {
            throw new ServiceException("用户已存在");
        }
        return baseMapper.insert(userInfo);
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {

        return baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username));
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        int i = baseMapper.updateById(userInfo);
        return i > 0 ? true : false;
    }

    @Override
    public UserInfoVo getUserInfo(Long userId) {

        return baseMapper.getUserInfo(userId);
    }

    @Override
    public Boolean isCollect(Long skuId, Long userId) {
        String cacheKey = "user:collect:" + userId;
        List<Long> skuIdList =(List<Long>) redisTemplate.opsForValue().get(cacheKey);
        if(CollectionUtils.isEmpty(skuIdList)){
            return false;
        }
        boolean contains = skuIdList.contains(skuId);
        return contains;
    }

    @Override
    public Boolean collect(Long skuId, Long userId) {
        String cacheKey = "user:collect:" + userId;
        List<Long> skuIdList =(List<Long>) redisTemplate.opsForValue().get(cacheKey);
        if (CollectionUtils.isEmpty(skuIdList)){
            skuIdList = new ArrayList<>();
        }
        skuIdList.add(skuId);
        redisTemplate.opsForValue().set(cacheKey,skuIdList);
        return true;
    }

    @Override
    public Boolean cancelCollect(Long skuId, Long userId) {
        String cacheKey = "user:collect:" + userId;
        List<Long> skuIdList =(List<Long>) redisTemplate.opsForValue().get(cacheKey);
        if (CollectionUtils.isEmpty(skuIdList)){
            throw new ServiceException("收藏列表为空");
        }
        skuIdList.remove(skuId);
        redisTemplate.opsForValue().set(cacheKey,skuIdList);
        return true;
    }

}
